package com.yphone.ui.pay.presenter;

import android.util.Log;

import com.google.gson.reflect.TypeToken;
import com.yphone.net.HttpConstants;
import com.yphone.net.IUIDataListener;
import com.yphone.net.bean.BaseResultWrapper;
import com.yphone.net.helper.ActivityHttpHelper;
import com.yphone.tools.MyLog;
import com.yphone.tools.ToastUtil;
import com.yphone.ui.dialog.CommSmallLoadingDialog;
import com.yphone.ui.login.manager.LoginManager;
import com.yphone.ui.pay.bean.PayInfo;
import com.yphone.ui.pay.contract.PayActivityContract;
import com.yphone.ui.pay.model.PayViewModel;

import java.util.HashMap;
import java.util.List;

public class PayPresenter implements PayActivityContract.IPresenter {

    private final PayActivityContract.IView view;
    private PayViewModel model;

    public PayPresenter(PayActivityContract.IView view){
        this.view = view;
    }

    @Override
    public void load() {

        model.request(new IUIDataListener() {
            @Override
            public void uiDataSuccess(Object obj) {
                BaseResultWrapper<List<PayInfo>> wrapper = (BaseResultWrapper) obj;
                MyLog.e("pay code = "+wrapper.code);
                if (wrapper.code == 1001){
                    view.logout();
                    return;
                }
                List<PayInfo> data = wrapper.data;
                view.success(data);
                CommSmallLoadingDialog.dismissDialog();
            }

            @Override
            public void uiDataError(Exception exc) {
                MyLog.e(Log.getStackTraceString(exc));
                CommSmallLoadingDialog.dismissDialog();
            }
        });
    }

    @Override
    public void queryOrder(IUIDataListener iuiDataListener,int num) {
        ActivityHttpHelper<BaseResultWrapper<String>> helper = new ActivityHttpHelper<>(new TypeToken<BaseResultWrapper<String>>() {
        });
        HashMap<String, String> map = new HashMap<>();
        map.put("token", LoginManager.getInstance().getToken());
        map.put("type", "1");
        map.put("num",num+"");
        helper.UpdateUIDataListener(iuiDataListener);
        helper.sendPostRequest(HttpConstants.getOrderNo,map,HttpConstants.TIME_OUT3);
    }

    @Override
    public void destory() {

    }

    @Override
    public void start() {
        model = new PayViewModel();
    }
}
